package com.springboot.cloud.sysadmin.organization.rest;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.core.utils.StringUtils;
import com.springboot.cloud.sysadmin.organization.entity.form.PositionForm;
import com.springboot.cloud.sysadmin.organization.entity.form.PositionQueryForm;
import com.springboot.cloud.sysadmin.organization.entity.param.PositionQueryParam;
import com.springboot.cloud.sysadmin.organization.entity.po.Position;
import com.springboot.cloud.sysadmin.organization.service.IPositionService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by ZengJian on 2021/2/3 0003 上午 9:22
 */
@RestController
@RequestMapping("/position")
@Api(tags = "职位")
@Slf4j

public class PositionController {

    @Autowired
    private   IPositionService positionService;

    @ApiOperation(value = "新增职位", notes = "新增一个职位")
    @ApiImplicitParam(name = "positionForm", value = "新增职位form表单", required = true, dataType = "PositionForm")
    @PostMapping
    public Result add(@Valid @RequestBody PositionForm positionForm) {
        log.debug("name:{}", positionForm);
        Position position = positionForm.toPo(Position.class);
        if (positionService.add(position)) {
            return Result.success("添加职位成功");
        } else {
            return Result.error("已存在相同名称的职位");
        }
    }

    @ApiOperation(value = "删除职位", notes = "根据url的id来指定删除对象")
    @DeleteMapping
    public Result delete(@Valid @RequestBody Set<String> ids) {
        if (StringUtils.isEmpty(ids)) {
            return Result.error("没选中要删除的职位");
        } else if (positionService.delete(ids)) {
            return Result.success("删除职位成功");
        }
        return Result.error("没选中要删除的职位");

    }

    @ApiOperation(value = "修改职位", notes = "修改指定职位信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "职位ID", required = true, dataType = "string"),
            @ApiImplicitParam(name = "positionForm", value = "职位实体", required = true, dataType = "PositionForm")
    })
    @PutMapping(value = "/{id}")
    public Result update(@PathVariable String id, @Valid @RequestBody PositionForm positionForm) {
        if (StrUtil.isBlank(id)) {
            return Result.error("id不可为空");
        }
        Position position = positionForm.toPo(Position.class);
        position.setId(id);
        if (positionService.update(position)) {
            return Result.success("修改职位成功");
        }
        return Result.error("更新失败");
    }

    @ApiOperation(value = "获取职位", notes = "获取指定职位信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "职位ID", required = true, dataType = "string")
    @GetMapping(value = "/{id}")
    public Result get(@PathVariable String id) {
        if (StrUtil.isBlank(id)) {
            return Result.error("没有职位的id");
        }
        log.info("get with id:{}", id);
        if (positionService.get(id)) {
            return Result.success(positionService.getPosition(id));
        }
        return Result.error("查找不到对应值");
    }

    @ApiOperation(value = "获取所有职位", notes = "获取所有职位")
    @GetMapping(value = "/all")
    public Result get() {
        return Result.success(positionService.getAll());
    }

    @ApiOperation(value = "查询职位", notes = "根据用户id查询用户所拥有的职位信息")
    @ApiImplicitParam(paramType = "path", name = "userId", value = "用户id", required = true, dataType = "string")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping(value = "/user/{userId}")
    public Result queryById(@PathVariable String userId) {
        if (StrUtil.isBlank(userId)) {
            return Result.error("没有职位的id，无法查询");
        }
        if (CollectionUtils.isEmpty(positionService.query(userId))) {
            return Result.error("查找不到对应值");
        }
        return Result.success(positionService.query(userId));

    }

    @ApiOperation(value = "搜索职位", notes = "根据条件搜索职位信息")
    @ApiImplicitParam(name = "positionQueryForm", value = "职位查询参数", required = true, dataType = "PositionQueryForm")
    @PostMapping(value = "/conditions")
    public Result query(@Valid @RequestBody PositionQueryForm positionQueryForm) {
        if (positionQueryForm.getCreatedTime() != null && !positionQueryForm.getCreatedTime().isEmpty()) {
            positionQueryForm.setCreatedTimeStart(positionQueryForm.getCreatedTime().get(0));
            positionQueryForm.setCreatedTimeEnd(positionQueryForm.getCreatedTime().get(1));
        }
        log.debug("query with name:{}", positionQueryForm);
        return Result.success(positionService.query(positionQueryForm.getPage(), positionQueryForm.toParam(PositionQueryParam.class)));
    }

    @ApiOperation(value = "查询职位", notes = "根据条件查询职位信息，简单查询")
    @ApiImplicitParam(paramType = "query", name = "name", value = "职位名称", required = true, dataType = "string")
    @GetMapping
    public Result query(@RequestParam String name) {
        if (StrUtil.isBlank(name)) {
            return Result.error("职位名称不可为空值");
        }
        log.debug("query with name:{}", name);
        PositionQueryParam positionQueryParam = new PositionQueryParam();
        positionQueryParam.setName(name);
        return Result.success(positionService.query(positionQueryParam));

    }
}
